

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class Emetteur extends Thread
 {
  private Tampon  tamponDEmission;
  private Process process;
  private int     numPort;
    
  public Emetteur(Process _process, int _numPort)
   {
    this.process = _process;
    this.tamponDEmission = this.process.getTamponDEmission();
    this.numPort = _numPort;
   }
    
  @Override
  public void run()
   {
    try
     {
      Socket             socket       = new Socket (InetAddress.getByName("localhost"), numPort);
      ObjectOutputStream fluxDeSortie = new ObjectOutputStream (socket.getOutputStream());
      System.out.println (process.getNumProcess() + " : Emetteur  : Emeteur connecte au recepteur");
	  boolean test=true;		
      while (test)
       {               
        //System.out.println (process.getNumProcess() + " : Emetteur  : Attente message a envoyer");
        Message message = this.tamponDEmission.getElementTampon();
    	fluxDeSortie.writeObject (message);        
        fluxDeSortie.flush();
        MessageUtile messageTest = (MessageUtile) message;
        if(messageTest.getContenu().equals("Fin"))
        {
        	System.out.println("Fermeture du thread emetteur du processus " + process.getNumProcess());
        	test=false;
        }
        //System.out.println (process.getNumProcess() + " : Emetteur  : Message envoye");
       }

      //socket.close();
     }
    catch (Exception e)
     {
      e.printStackTrace();
     }
   }
 }
